<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/12/15
 * Time: 14:40
 */

namespace app\api\model;


use app\admin\model\BydDesigner;
use app\admin\model\BydDesignerPro;
use app\admin\model\BydDesMode;
use app\admin\model\BydDestri;
use app\admin\model\BydDispro;
use app\admin\model\BydDistri;
use app\admin\model\BydIden;
use app\admin\model\BydUserSale;
use think\Model;

class BydIdentity extends Model
{
    // 表名
    protected $name = 'byd_identity';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [

    ];

    //查找上级b1函数
    public static function findbone($data){
        $find=BydDistri::where('user_id',$data)->find();//查找pid得family
        if($find['family']==0){
            return  0;
        }else{
            return $find['family'];
        }

//        $user=db('BydUser')->where('id',$data)->find();//用户表查找此人身份

//        if($user['bbs']==2){
//            return $user['id'];
//        }else{
//            $find=BydDistri::where('user_id',$data)->find();
//            if($find['family']>0){
//                $finds=BydIdentity::findbone($find['user_id']);
//            }else{
//                return  0;
//            }
//        }

    }
    //查找上级d2函数
    public static function findbtwo($data)
    {
        $find = BydDestri::where('user_id', $data)->find();//查找pid得family

        if ($find['family']==0) {
            return  0;
        } else {
            return $find['family'];
        }
    }

    /**
     * 申请成为设计师
     */
    public static function adddes($data){
        if(!empty($data['mobile'])){
            $qq=preg_match_all("/^1[34578]\d{9}$/", $data['mobile'], $mobiles);
            if($qq==0){
                return false;
            }
        }
         $dele=BydDesigner::where('user_id',$data['user_id'])->where('state',2)->delete();

        $add=BydDesigner::create($data);
        if($add){
            return $add;
        }else{
            return null;
        }
    }
    public static function checkIdCard($idcard){

// 只能是18位

        if(strlen($idcard)!=18){

            return false;

        }

// 取出本体码

        $idcard_base = substr($idcard, 0, 17);

// 取出校验码

        $verify_code = substr($idcard, 17, 1);

// 加权因子

        $factor = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);

// 校验码对应值

        $verify_code_list = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');

// 根据前17位计算校验码

        $total = 0;

        for($i=0; $i<17; $i++){

            $total += substr($idcard_base, $i, 1)*$factor[$i];

        }

// 取模

        $mod = $total % 11;

// 比较校验码

        if($verify_code == $verify_code_list[$mod]){

            return true;

        }else{

            return false;

        }

    }
    /**
     * 设计师上传方案
     */
    public static function addpro($data){

        $add=BydDesignerPro::create($data);
        if($add){
            return $add;
        }else{
            return null;
        }
    }


    /**
     * 我的团队
     * 用户id   user_id
     * 级别     state 1为1级2为2级
     */
    public static function myteam($data){

        if($data['state']==1){
            $arr=BydDistri::where('pid',$data['user_id'])->select();
        }else{
            $arr=BydDistri::where('family',$data['user_id'])->select();
        }
        foreach ($arr as &$v){
            $v['users']=\app\admin\model\BydUser::find($v['user_id']);
            $v['moneys']=BydDispro::where('pid',$data['user_id'])->where('user_id',$v['user_id'])->sum('price');
            $v['son']=BydDistri::whereOr(
                ['pid'=>$v['user_id'],
                    'family'=>$v['user_id'],
                ])->count();
        }
        $arr['count']=BydDistri::whereOr(
            ['pid'=>$data['user_id'],
                'family'=>$data['user_id'],
            ])->count();//总人数whereOr(

        $arr['sum']=BydDispro::where(
            ['pid'=>$data['user_id'],
            ])->sum('price');//总返利


        if($arr){
            return $arr;
        }else{
            return null;
        }
    }
    /**
     *    设计师/销售员 我的佣金
     */
    public static function mysub($data){
      $arr=BydDispro::where('pid',$data['user_id'])->select();
      $arr['allprice']=BydDispro::where('pid',$data['user_id'])->sum('price');
        $arr['canprice']=BydDispro::where('pid',$data['user_id'])->where('tixian',0)->sum('price');
      if($arr){
          return $arr;
      }else{
          return null;
      }
    }
    /**
     * 销售员拉新返佣
     * 1：bs 2:b1
     * 身份id  idcar
     * 用户id   user_id
     *
     */
    public static function xsub($data){
        //微信支付成功后

        $osn = date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);//订单号
        $user=new BydUser();
        $save=$user->where('id',$data['user_id'])->update(['bbs'=>$data['idcar']]);
        $sale=BydUserSale::where('user_id',$data['user_id'])->find();
        if(!empty($sale)){
            $saleedit=BydUserSale::where('user_id',$data['user_id'])->update(['level'=>$data['idcar']]);
        }
        $find=\app\admin\model\BydIdentity::where('id',$data['idcar'])->find();//查询身份分佣金额
        $rand=rand(1,9999);
        $head=$find['idenhead'];
        $num="$head$rand";
        $num="b-" .  str_pad(rand(1,9999), 5, '0',  STR_PAD_LEFT);
        $dis=BydDistri::where('user_id',$data['user_id'])->find();//查询上级

        if($dis){
            if($dis['family']>0){

//                $father=\app\admin\model\BydUser::where('id',$data['pid'])->find();//查看父级身份
//                if($father['bbs']>0){
//                    $level=\app\admin\model\BydIdentity::find($father['bbs']);
//                    $ftc=$order['price']*$level['myproduct']/100;//上级所得提成
//                }
////                    $ftc=$order['price']*$level['myproduct']/100;//上级所得提成
//                $gfather=\app\admin\model\BydUser::where('id',$pro['family'])->find();//查看第一级别身份
//                $levels=\app\admin\model\BydIdentity::find($gfather['bbs']);

                $pid=\app\admin\model\BydUser::find($dis['pid']);
                $familyuser=BydUserSale::where('user_id',$dis['family'])->find();//查看第一级身份
                if(empty($familyuser)){
                    //family为c时候
                    //$disupdate=BydDistri::where('user_id',$data['user_id'])->update(['family'  =>0,'pid'=>0]);//自己独立线出来
                    if($pid['bbs']>0){
                        $pidmoney=BydDistri::where('user_id',$dis['pid'])->find();
                        $add = new BydDispro();
                        $add->data([
                            'cjprice'=>$find['money'],
                            'level'=>$data['idcar'],
                            'pid'  => $dis['pid'],
                            'state' =>  1,
                            'user_id' =>  $data['user_id'],
                            'type' => 1,
                            'order_id'=>$osn,
                            'money'=>0,
                            'price'=>$pidmoney['mynewmoney']
                        ]);
                        $add->save();//记录c端用户消费
                                            $addsse = new BydUserSale();
                    $addsse->data([
                        'state' =>  1,
                        'user_id' =>  $data['user_id'],
                        'level'=>$data['idcar'],
                        'bh'=>$num
                    ]);
                    $addsse->save();
                    }else{
                        $add = new BydDispro();
                        $add->data([
                            'cjprice'=>$find['money'],
                            'level'=>$data['idcar'],
                            'pid'  => 0,
                            'state' =>  1,
                            'user_id' =>  $data['user_id'],
                            'type' => 1,
                            'order_id'=>$osn,
                            'money'=>0,
                            'price'=>0
                        ]);
                        $add->save();//记录c端用户消费
                                            $addsse = new BydUserSale();
                    $addsse->data([
                        'state' =>  1,
                        'user_id' =>  $data['user_id'],
                        'level'=>$data['idcar'],
                        'bh'=>$num
                    ]);
                    $addsse->save();
                    }

                    $diss=new BydDistri();
                    if($data['idcar']==2){
                        $myself=$diss->where('user_id',$data['user_id'])->update(['family'=>0,'pid'=>0]);//自己独立线出来
                        //$mybbs=\app\admin\model\BydUser::where('id',$data['user_id'])->update
                    }
                    $where['family']=array('eq',$dis['family']);
                    $where['id']=array('gt',$dis['id']);
                    //$where['pid']=array('eq',$data['user_id']);
//                        $where[]=['family','=',$dis['family']];
                    //$wheres['pid']=['eq',$data['user_id']];
                    $pid=$diss->isUpdate(true,$where)->save(['family'=>$data['user_id']]);//改下面得下级

                }
                if($familyuser['level']==2){
                    if($data['idcar']==1){
                        //有b1上级且购买bs
                        if($dis['family']==$dis['pid']){
                            //第一级与第二级为同一人
                            $price=BydIdentity::find(2);
                            $add = new BydDispro();
                            $add->data([
                                'cjprice'=>$find['money'],
                                'level'=>$data['idcar'],
                                'pid'  => $dis['pid'],
                                'state' =>  1,
                                'user_id' =>  $data['user_id'],
                                'type' => 1,
                                'order_id'=>$osn,
                                'money'=>0,
                                'price'=>$price['mynewmoney']
                            ]);
                            $add->save();
                            if($add){
                                $adds = new BydUserSale();
                                $adds->data([
                                    'state' =>  0,
                                    'user_id' =>  $data['user_id'],
                                    'level'=>$data['idcar'],
                                    'bh'=>$num
                                ]);
                                $adds->save();
                                $sumcom=$user->where('id',$dis['pid'])->update(['bbs'=>$data['idcar']]);//修改身份标识
                            }

                            //$sumcoms=$user->where('id',$dis['pid'])->setInc('cancommission',$price['mynewmoney']);//可提现用金+40
                        }else{
                            $piduser=\app\admin\model\BydUser::find($dis['pid']);//查看pid的身份
                            //第family上级分佣
                            $oneprice=BydIdentity::find(2);
                            $twoprice=BydIdentity::find(1);
                            if($piduser['bbs']==1){//如果为bs
                                //第pid上级分佣
                                $user = new BydDispro();
                                $list = [
                                    ['pid'=>$dis['family'],'state'=>1,'user_id'=>$data['user_id'],'type'=>1,'order_id'=>$osn,'price'=>$oneprice['mysonmoney'],'money'=>1],
                                    ['pid'=>$dis['pid'],'state'=>1,'user_id'=>$data['user_id'],'type'=>1,'order_id'=>$osn,'price'=>$twoprice['mynewmoney'],'money'=>0]
                                ];
                                $user->saveAll($list);
                                $adds = new BydUserSale();
                                $adds->data([
                                    'state' =>  0,
                                    'user_id' =>  $data['user_id'],
                                    'level'=>$data['idcar'],
                                    'bh'=>$num
                                ]);
                                $adds->save();
                                $sumcom=\app\admin\model\BydUser::where('id',$data['user_id'])->update(['bbs'=>$data['idcar']]);//修改身份标识
                                $sumcoms=\app\admin\model\BydDistri::where('user_id',$data['user_id'])->update(['pid'=>$dis['family']]);//修改退队关系列表
                            }else{
                                //pid为c端用户
                                $add = new BydDispro();
                                $add->data([
                                    'cjprice'=>$twoprice['money'],
                                    'level'=>$data['idcar'],
                                    'pid'  => $dis['family'],
                                    'money'=>1,
                                    'state' =>  1,
                                    'user_id' =>  $data['user_id'],
                                    'type' => 1,
                                    'tixian' => 0,
                                    'order_id'=>$osn,
                                    'price'=>$oneprice['mynewmoney'],
                                ]);
                                $add->save();//给b1的分佣
                                $addsqw = new BydUserSale();
                                $addsqw->data([
                                    'state' =>  1,
                                    'user_id' =>  $data['user_id'],
                                    'level'=>$data['idcar'],
                                    'bh'=>$num
                                ]);
                                $addsqw->save();
                                $sumcoms=\app\admin\model\BydDistri::where('user_id',$data['user_id'])->update(['pid'=>$dis['family']]);//修改退队关系列表
                                $sumcom=\app\admin\model\BydUser::where('id',$data['user_id'])->update(['bbs'=>$data['idcar']]);//修改身份标识
                            }


//                            $upsumcom=$user->where('id',$dis['pid'])->setInc('commission',$twoprice['mynewmoney']);//上级获得总用金+30
//                            $upsumcoms=$user->where('id',$dis['pid'])->setInc('cancommission',$twoprice['mynewmoney']);//上级获得可提现用金+30
//                            $uppsumcom=$user->where('id',$dis['family'])->setInc('commission',$oneprice['mysonmoney']);//上级获得总用金+10
//                            $uppsumcoms=$user->where('id',$dis['family'])->setInc('cancommission',$oneprice['mysonmoney']);//上级获得可提现用金+10
                        }
                    }else{

                        //有b1上级且购买b1
                        $price=BydIdentity::find(2);

                        $add = new BydDispro();
                        $add->data([
                            'cjprice'=>$find['money'],
                            'level'=>$data['idcar'],
                            'pid'  => $dis['family'],
                            'state' =>  1,
                            'user_id' =>  $data['user_id'],
                            'type' => 1,
                            'order_id'=>$osn,
                            'money'=>1,
                            'price'=>$price['mysonup']
                        ]);
                        $add->save();

                        if($add){

                            $adds = new BydUserSale();
                            $adds->data([
                                'state' =>  0,
                                'user_id' =>  $data['user_id'],
                                'level'=>$data['idcar'],
                                'bh'=>$num
                            ]);
                            $adds->save();
                            $sumcom=$user->where('id',$data['user_id'])->update(['bbs'=>$data['idcar']]);//修改身份标识
                            $diss=new BydDistri();

                            $where['family']=array('eq',$dis['family']);
                            $where['id']=array('gt',$dis['id']);
                            //$where['pid']=array('eq',$data['user_id']);
//                        $where[]=['family','=',$dis['family']];
                     //$wheres['pid']=['eq',$data['user_id']];
                            $pid=$diss->isUpdate(true,$where)->save(['family'=>$data['user_id']]);//改下面得下级
                            $myself=$diss->where('user_id',$data['user_id'])->update(['family'=>0,'pid'=>0]);

                           // $pid=$diss->where($where)->find();


                        }
//                        $uppsumcom=$user->where('id',$dis['family'])->setInc('commission',$price['mysonup']);//上级获得总用金+700
//                        $uppsumcoms=$user->where('id',$dis['family'])->setInc('cancommission',$price['mysonup']);//上级获得可提现用金+700
//                        //购买后第二级为购买者的独立成一个团队

                    }
                }else{

                    if($data['idcar']==1){
                        $prices=BydIdentity::find(2);
                        $family=BydIdentity::findbone($dis['family']);

//                            print_r($family);die;
                        if($family>0){
                            $fambbs=\app\admin\model\BydUser::find($family);
                            if($fambbs['bbs']==2){
                                $adds = new BydDispro();
                                $adds->data([
                                    'money'=>1,
                                    'cjprice'=>$find['money'],
                                    'level'=>$data['idcar'],
                                    'pid'  =>$family,
                                    'state' =>  1,
                                    'user_id' =>  $data['user_id'],
                                    'type' => 1,
                                    'order_id'=>$osn,
                                    'tixian'=>0,
                                    'price'=>$prices['mysonmoney']
                                ]);
                                $adds->save();
                            }
                        }
                            //$sumcoms=BydDistri::where('user_id',$data['user_id'])->update(['family'=>$family]);//修改身份标识'pid'=>$family,
                            $diss=new BydDistri();
                            $where['family']=array('eq',$dis['family']);
                            $where['id']=array('gt',$dis['id']);
//                        $where[]=['family','=',$dis['family']];
                            //$where['pid']=['eq',$data['user_id']];
                            $pid=$diss->isUpdate(true,$where)->save(['family'=>$data['user_id']]);//改下面得下级

                        //只有第二级上级且购买bs
                        $price=BydIdentity::find(1);
                        $add = new BydDispro();
                        $add->data([
                            'money'=>0,
                            'cjprice'=>$find['money'],
                            'level'=>$data['idcar'],
                            'pid'  => $dis['family'],
                            'state' =>  1,
                            'user_id' =>  $data['user_id'],
                            'type' => 1,
                            'order_id'=>$osn,
                            'price'=>$price['mynewmoney']
                        ]);
                        $add->save();
                        if($add){
                            $adds = new BydUserSale();
                            $adds->data([
                                'state' =>  0,
                                'user_id' =>  $data['user_id'],
                                'level'=>$data['idcar'],
                                'bh'=>$num
                            ]);
                            $adds->save();
                            $sumcom=$user->where('id',$data['user_id'])->update(['bbs'=>$data['idcar']]);//修改身份标识
                            //查找family的b1

                        }
//                        $uppsumcom=$user->where('id',$dis['pid'])->setInc('commission',$price['mynewmoney']);//上级获得总用金+30
//                        $uppsumcoms=$user->where('id',$dis['pid'])->setInc('cancommission',$price['mynewmoney']);//上级获得可提现用金+30
                    }else{

                        //只有第二级上级且购买b1
                        $prices=BydIdentity::find(2);
                        $family=BydIdentity::findbone($dis['family']);
//                            print_r($family);die;
                        if($family>0){
                            $famibbs=\app\admin\model\BydUser::find($family);
                            if(!empty($famibbs)){
                                if($famibbs['bbs']==2){

                                    $adds = new BydDispro();
                                    $adds->data([
                                        'money'=>1,
                                        'cjprice'=>$find['money'],
                                        'level'=>$data['idcar'],
                                        'pid'  => $family,
                                        'state' =>  1,
                                        'user_id' =>  $data['user_id'],
                                        'type' => 1,
                                        'order_id'=>$osn,
                                        'tixian'=>0,
                                        'price'=>$prices['mysonup']
                                    ]);
                                    $adds->save();
                                }
                            }
                        }else{

                            $sumcoms=BydDistri::where('user_id',$data['user_id'])->update(['pid'=>$family,'family'=>$family]);//修改身份标识
                            $diss=new BydDistri();
                            $where['family']=array('eq',$dis['family']);
                            $where['id']=array('gt',$dis['id']);
//                        $where[]=['family','=',$dis['family']];
                            //$where['pid']=['eq',$data['user_id']];
                            $pid=$diss->isUpdate(true,$where)->save(['family'=>$data['user_id']]);//改下面得下级
                        }

                        $sale=new BydUserSale();
                        $find=$sale->where('user_id',$data['user_id'])->find();
                        if(empty($find)){
                            $addsqw = new BydUserSale();
                            $addsqw->data([
                                'state' =>  1,
                                'user_id' =>  $data['user_id'],
                                'level'=>$data['idcar'],
                                'bh'=>$num
                            ]);
                            $addsqw->save();
                        }else{
                            $pid=$sale->isUpdate(true,['user_id'=>$data['user_id']])->save(['level'=>$data['idcar'],'state'=>0]);
                        }
                        //购买后第二级为购买者的独立成一个团队
                        $diss=new BydDistri();
                        //$where['pid']=array('eq',$data['user_id']);
                        $where['family']=array('eq',$dis['family']);
                        $where['id']=array('gt',$dis['id']);
//                        $where[]=['family','=',$dis['family']];
                        //$where['user_id']=['eq',$data['user_id']];
                        $pid=$diss->isUpdate(true,$where)->save(['family'=>$data['user_id']]);//改下面得下级
                        $myself=$diss->where('user_id',$data['user_id'])->update(['family'=>0,'pid'=>0]);
                    }
                }
            }else{
                $userfind=\app\admin\model\BydUser::find($data['user_id']);
                if($userfind['bbs']==0){
                    //c端升bs
                    $adds = new BydUserSale();
                    $adds->data([
                        'state' =>  0,
                        'user_id' =>  $data['user_id'],
                        'level'=>$data['idcar'],
                        'bh'=>$num
                    ]);
                    $adds->save();
                    $sale=new BydUserSale();
                    $disfind=BydDistri::where('user_id',$data['user_id'])->update(['level'=>$data['idcar']]);
                }else{
                    //散户bs升b1
                    $sale=new BydUserSale();
                    $pid=$sale->isUpdate(true,['user_id'=>$data['user_id']])->save(['level'=>$data['idcar'],'state'=>0]);
                    $disfind=BydDistri::where('user_id',$data['user_id'])->update(['level'=>$data['idcar']]);
//                    $diss=new BydDistri();
//                    //$where['pid']=array('eq',$data['user_id']);
//                    $sonfind=$diss->where('pid',$data['user_id'])->select();
//                    if(!empty($sonfind)){
//                        foreach ($sonfind as $v){
//                            $where['family']=array('eq',$v['user_id']);
//                            $familyedit=$diss->isUpdate(true,$where)->save(['family'=>$data['user_id']]);//改下面得下级
//                        }
//                    }
//                    $where['family']=array('eq',$dis['family']);
//                    $where['id']=array('gt',$dis['id']);
////                        $where[]=['family','=',$dis['family']];
//                    //$where['user_id']=['eq',$data['user_id']];
//                    $pid=$diss->isUpdate(true,$where)->save(['family'=>$data['user_id']]);//改下面得下级
                }

            }

            }else{
            //散户自主购买
            if($data['idcar']==1){
                $data['level']=0;
                $data['state']=1;
            }else{
                $data['level']=$data['idcar'];
                $data['state']=0;
            }
            //添加到身份列表
                $add = new BydDistri();
                $add->data([
                    'pid'  => 0,
                    'family' =>  0,
                    'user_id' =>  $data['user_id'],
                    'level'=>$data['idcar'],
                ]);
                $add->save();
            //添加销售员列表
                $adds = new BydUserSale();
                $adds->data([
                    'state' =>  $data['state'],
                    'user_id' =>  $data['user_id'],
                    'level'=>$data['idcar'],
                    'bh'=>$num
                ]);
                $adds->save();
        }

    }
    /**
     * 设计师拉新返佣
     * 3：ds 4:d2
     * 身份id  idcar
     * 用户id   user_id
     *
     */
    public static function dessub($data){

        //审核通过后
        $osn = date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);//订单号
        $user=new BydUser();
       $deslevel=$user->where('id',$data['user_id'])->find();

       //$data['idcar']=$deslevel['des'];
        $dessq=$user->where('id',$data['user_id'])->update(['des'=>$data['idcar']]);//修改身份
        if($deslevel['bbs']==0){
            if($data['idcar']==4 || $data['idcar']==3){
                $findsale=BydUserSale::where('user_id',$data['user_id'])->find();
                if(empty($findsale)){
                    $num="b-" .  str_pad(rand(1,9999), 5, '0',  STR_PAD_LEFT);
                    $add=new BydUserSale();
                    $add->data([
                        'user_id'=>$data['user_id'],
                        'level'=>1,
                        'bh'=>$num,
                        'state'=>1
                    ]);
                    $add->save();
                    $addsq=new BydDistri();
                    $addsq->data([
                        'user_id'=>$data['user_id'],
                        'pid'=>0,
                        'family'=>0,
                        'level'=>1
                    ]);
                    $addsq->save();
                }

                $sale=BydUserSale::where('user_id',$data['user_id'])->update(['level'=>1]);
                $bbs=$user->where('id',$data['user_id'])->update(['bbs'=>1]);//修改身份
            }

        }
//        if($data['idcar']==4){
//            $bbs=BydUser::where('id',$data['user_id'])->update(['bbs'=>3]);//修改身份
//            $findsale=BydUserSale::where('user_id',$data['user_id'])->find();
//            if(empty($findsale)){
//                $num="b-" .  str_pad(rand(1,9999), 5, '0',  STR_PAD_LEFT);
//                $add=new BydUserSale();
//                $add->data([
//                    'user_id'=>$data['user_id'],
//                    'level'=>3,
//                    'bh'=>$num,
//                    'state'=>1
//                ]);
//                $add->save();
//                $addqew=new BydDistri();
//                $addqew->data([
//                    'user_id'=>$data['user_id'],
//                    'pid'=>0,
//                    'family'=>0,
//                    'level'=>3
//                ]);
//                $addqew->save();
//            }
//                $sale=BydUserSale::where('user_id',$data['user_id'])->update(['level'=>3]);
//
//
//        }


        $find=\app\admin\model\BydIdentity::where('id',$data['idcar'])->find();//查询身份分佣金额
        $rand=rand(1,9999);
        $head=$find['idenhead'];
        $num="$head$rand";
        $num="d-" .  str_pad(rand(1,9999), 5, '0',  STR_PAD_LEFT);
        $dis=BydDestri::where('user_id',$data['user_id'])->find();//查询上级
//        print_r($dis);die;
        if($dis){
            if($dis['family']>0){
                $familyuser=\app\admin\model\BydUser::find($dis['family']);//查看第一级身份
                if($familyuser['des']==4){
                    if($data['idcar']==3){
                        //有d2上级且购买ds
                        if($dis['family']==$dis['pid']){
                            //第一级与第二级为同一人
                            $price=BydIdentity::find(4);
                            $add = new BydDispro();
                            $add->data([
                                'cjprice'=>$find['money'],
                                'level'=>$data['idcar'],
                                'pid'  => $dis['pid'],
                                'state' =>  1,
                                'user_id' =>  $data['user_id'],
                                'type' => 1,
                                'order_id'=>$osn,
                                'money'=>0,
                                'price'=>$price['mynewmoney']
                            ]);
                            $add->save();
                            $diswhere=BydDistri::where('user_id',$data['user_id'])->find();//查询上级
                            if(empty($diswhere)){
                                $adds = new BydUserSale();
                                $adds->data([
                                    'state' =>  1,
                                    'user_id' =>  $data['user_id'],
                                    'level'=>1,

                                ]);
                                $adds->save();//同时成为bs
                                //查找销售端团队表
                                $bbsfind=\app\admin\model\BydUser::find($data['user_id']);
                                if(empty($bbsfind)){
                                    $adds = new BydUserSale();
                                    $adds->data([
                                        'state' =>  1,
                                        'user_id' =>  $data['user_id'],
                                        'level'=>1,
                                        'bh'=>$num
                                    ]);
                                    $adds->save();//同时成为bs
                                    $addss = new BydDistri();
                                    $addss->data([
                                        'level'=>$data['idcar'],
                                        'pid'  => 0,
                                        'family' =>  0,
                                        'user_id' =>  $data['user_id'],
                                    ]);
                                    $addss->save();
                                }else{
                                    $arr=[
                                      'idcar'=>3,
                                      'user_id'=>$data['user_id']
                                    ];
                                    $fy = \app\api\model\BydIdentity::xsub($arr);
                                }

                            }

                            $sumcom=$user->where('id',$dis['pid'])->setInc('commission',$price['mynewmoney']);//总用金+40
                            $sumcoms=$user->where('id',$dis['pid'])->setInc('cancommission',$price['mynewmoney']);//可提现用金+40
                        }else{
                            $piduser=\app\admin\model\BydUser::find($dis['pid']);//查看pid的身份
                            //第family上级分佣
                            $oneprice=BydIdentity::find(4);
                            $twoprice=BydIdentity::find(3);
                            if($piduser['des']==3){//如果为bs
                                //第pid上级分佣
                                $user = new BydDispro();
                                $list = [
                                    ['pid'=>$dis['family'],'state'=>1,'user_id'=>$data['user_id'],'type'=>1,'order_id'=>$osn,'price'=>$oneprice['mysonmoney'],'money'=>1,'level'=>$data['idcar']],
                                    ['pid'=>$dis['pid'],'state'=>1,'user_id'=>$data['user_id'],'type'=>1,'order_id'=>$osn,'price'=>$twoprice['mynewmoney'],'money'=>0,'level'=>$data['idcar']]
                                ];
                                $user->saveAll($list);
                                $bbsfind=\app\admin\model\BydUser::find($data['user_id']);
                                if(empty($bbsfind)){
                                    $adds = new BydUserSale();
                                    $adds->data([
                                        'state' =>  1,
                                        'user_id' =>  $data['user_id'],
                                        'level'=>1,
                                    ]);
                                    $adds->save();//同时成为bs
                                    $addss = new BydDistri();
                                    $addss->data([
                                        'level'=>1,
                                        'pid'  => 0,
                                        'family' =>  0,
                                        'user_id' =>  $data['user_id'],
                                    ]);
                                    $addss->save();
                                }else{
                                    $arr=[
                                        'idcar'=>3,
                                        'user_id'=>$data['user_id']
                                    ];
                                    $fy = \app\api\model\BydIdentity::xsub($arr);
                                }
//                                $adds = new BydUserSale();
//                                $adds->data([
//                                    'state' =>  0,
//                                    'user_id' =>  $data['user_id'],
//                                    'level'=>$data['idcar'],
//                                ]);
//                                $adds->save();
                                $sumcom=\app\admin\model\BydUser::where('id',$data['user_id'])->update(['des'=>$data['idcar']]);//修改身份标识
                                $sumcoms=\app\admin\model\BydDestri::where('user_id',$data['user_id'])->update(['pid'=>$dis['family']]);//修改退队关系列表
                            }else{
                                //pid为c端用户
                                $add = new BydDispro();
                                $add->data([
                                    'cjprice'=>$twoprice['money'],
                                    'level'=>$data['idcar'],
                                    'pid'  => $dis['family'],
                                    'money'=>1,
                                    'state' =>  1,
                                    'user_id' =>  $data['user_id'],
                                    'type' => 1,
                                    'tixian' => 0,
                                    'order_id'=>$osn,
                                    'price'=>$oneprice['mysonmoney'],
                                ]);
                                $add->save();//给b1的分佣
                                $sumcoms=\app\admin\model\BydDestri::where('user_id',$data['user_id'])->update(['pid'=>$dis['family']]);//修改退队关系列表
                                $sumcom=\app\admin\model\BydUser::where('id',$data['user_id'])->update(['des'=>$data['idcar']]);//修改身份标识
                            }

                            //////////
//                            //第一级上级分佣
//                            $oneprice=BydIdentity::find(4);
//                            //第二级上级分佣
//                            $twoprice=BydIdentity::find(3);
//                            $user = new BydUser();
//                            $list = [
//                                ['pid'=>$dis['family'],'state'=>1,'user_id'=>$data['user_id'],'type'=>1,'order_id'=>$osn,'price'=>$oneprice['mysonmoney'],'money'=>1],
//                                ['pid'=>$dis['pid'],'state'=>1,'user_id'=>$data['user_id'],'type'=>1,'order_id'=>$osn,'price'=>$twoprice['mynewmoney'],'money'=>0]
//                            ];
//                            $user->saveAll($list);
//                            $upsumcom=$user->where('id',$dis['pid'])->setInc('commission',$twoprice['mynewmoney']);//上级获得总用金+30
//                            $upsumcoms=$user->where('id',$dis['pid'])->setInc('cancommission',$twoprice['mynewmoney']);//上级获得可提现用金+30
//                            $uppsumcom=$user->where('id',$dis['family'])->setInc('commission',$oneprice['mysonmoney']);//上级获得总用金+10
//                            $uppsumcoms=$user->where('id',$dis['family'])->setInc('cancommission',$oneprice['mysonmoney']);//上级获得可提现用金+10
                        }
                    }else{
                        //有d2上级且购买d2
                        $price=BydIdentity::find(4);
                        $add = new BydDispro();
                        $add->data([
                            'cjprice'=>$find['money'],
                            'level'=>$data['idcar'],
                            'pid'  => $dis['family'],
                            'state' =>  1,
                            'user_id' =>  $data['user_id'],
                            'type' => 1,
                            'order_id'=>$osn,
                            'money'=>1,
                            'price'=>$price['mysonup']
                        ]);
                        $add->save();
                        $uppsumcom=$user->where('id',$dis['family'])->setInc('commission',$price['mysonup']);//上级获得总用金+700
                        $uppsumcoms=$user->where('id',$dis['family'])->setInc('cancommission',$price['mysonup']);//上级获得可提现用金+700
                        //购买后第二级为购买者的独立成一个团队
                        $diss=new BydDestri();
                        //$where['pid']=array('eq',$data['user_id']);
                        $where['family']=array('eq',$dis['family']);
                        $where['id']=array('gt',$dis['id']);
//                        $where[]=['family','=',$dis['family']];
                        $wheres['user_id']=['eq',$data['user_id']];
                        $pid=$diss->isUpdate(true,$where)->save(['family'=>$data['user_id']]);//改下面得下级
                        $myself=$diss->where('user_id',$data['user_id'])->update(['family'=>0,'pid'=>0]);
                    }
                }else{

                    if($data['idcar']==3){
                        //只有第二级上级且购买ds
                        $price=BydIdentity::find(3);
                        $prices=BydIdentity::find(4);

                        $findtwo=BydIdentity::findbtwo($dis['family']);

                        if($findtwo>0){
                            $adds = new BydDispro();
                            $adds->data([
                                'money'=>1,
                                'cjprice'=>$find['money'],
                                'level'=>$data['idcar'],
                                'pid'  => $findtwo,
                                'state' =>  1,
                                'user_id' =>  $data['user_id'],
                                'type' => 1,
                                'order_id'=>$osn,
                                'tixian'=>0,
                                'price'=>$prices['mysonup']
                            ]);
                            $adds->save();
                        }
                        $sumcoms=BydDestri::where('user_id',$data['user_id'])->update(['pid'=>$findtwo,'family'=>$findtwo]);//修改身份标识
                        $diss=new BydDestri();
                        $where['family']=array('eq',$dis['family']);
                        $where['id']=array('gt',$dis['id']);
//                        $where[]=['family','=',$dis['family']];
                        //$where['pid']=['eq',$data['user_id']];
                        $pid=$diss->isUpdate(true,$where)->save(['family'=>$data['user_id']]);//改下面得下级
                        $add = new BydDispro();
                        $add->data([
                            'cjprice'=>$find['money'],
                            'level'=>$data['idcar'],
                            'pid'  => $dis['family'],
                            'state' =>  1,
                            'user_id' =>  $data['user_id'],
                            'type' => 1,
                            'order_id'=>$osn,
                            'price'=>$price['mynewmoney']
                        ]);
                        $add->save();
                        $diswhere=BydDistri::where('user_id',$data['user_id'])->find();//查询上级
                        if(empty($diswhere)){
                            $adds = new BydUserSale();
                            $adds->data([
                                'state' =>  1,
                                'user_id' =>  $data['user_id'],
                                'level'=>1,
                            ]);
                            $adds->save();//同时成为bs
                            $addss = new BydDistri();
                            $addss->data([
                                'level'=>$data['idcar'],
                                'pid'  => 0,
                                'family' =>  0,
                                'user_id' =>  $data['user_id'],
                            ]);
                            $addss->save();
                        }else{
                            $arr=[
                                'idcar'=>3,
                                'user_id'=>$data['user_id']
                            ];
                            $fy = \app\api\model\BydIdentity::xsub($arr);
                        }
//                        $uppsumcom=$user->where('id',$dis['pid'])->setInc('commission',$price['mynewmoney']);//上级获得总用金+30
//                        $uppsumcoms=$user->where('id',$dis['pid'])->setInc('cancommission',$price['mynewmoney']);//上级获得可提现用金+30
                    }else{
                        $prices=BydIdentity::find(4);
                        $findtwo=BydIdentity::findbtwo($dis['family']);
                        if($findtwo>0){
                            $adds = new BydDispro();
                            $adds->data([
                                'money'=>1,
                                'cjprice'=>$find['money'],
                                'level'=>$data['idcar'],
                                'pid'  => $findtwo,
                                'state' =>  1,
                                'user_id' =>  $data['user_id'],
                                'type' => 1,
                                'order_id'=>$osn,
                                'tixian'=>0,
                                'price'=>$prices['mysonup']
                            ]);
                            $adds->save();
                        }
                        //只有第二级上级且购买d1
                        //购买后第二级为购买者的独立成一个团队
                        $diss=new BydDestri();
                        //$where['pid']=array('eq',$data['user_id']);
                        $where['family']=array('eq',$dis['family']);
                        $where['id']=array('gt',$dis['id']);
//                        $where[]=['family','=',$dis['family']];
                        $wheres['user_id']=['eq',$data['user_id']];
                        $pid=$diss->isUpdate(true,$where)->save(['family'=>$data['user_id']]);//改下面得下级
                        $myself=$diss->where('user_id',$data['user_id'])->update(['family'=>0,'pid'=>0]);
                    }
                }
            }else{
                //散户ds升d2
                $sale=new BydDesigner();
                $pid=$sale->isUpdate(true,['user_id'=>$data['user_id']])->save(['level'=>$data['idcar']]);
                $piddes=$user->isUpdate(true,['id'=>$data['user_id']])->save(['des'=>$data['idcar']]);
            }
        }else{
            $diswhere=BydDistri::where('user_id',$data['user_id'])->find();//查询上级
            if(empty($diswhere)){
                $adds = new BydUserSale();
                $adds->data([
                    'state' =>  1,
                    'user_id' =>  $data['user_id'],
                    'level'=>1,
                ]);
                $adds->save();//同时成为bs
                $addss = new BydDistri();
                $addss->data([
                    'level'=>$data['idcar'],
                    'pid'  => 0,
                    'family' =>  0,
                    'user_id' =>  $data['user_id'],
                ]);
                $addss->save();
            }else{
                $arr=[
                    'idcar'=>1,
                    'user_id'=>$data['user_id']
                ];
                $fy = \app\api\model\BydIdentity::xsub($arr);
            }
            $add = new BydDestri();
            $add->data([
                'level'=>$data['idcar'],
                'pid'  => 0,
                'family' =>  0,
                'user_id' =>  $data['user_id'],
            ]);
            $add->save();
        }

    }
/////
    /**
     * 销售员拉新返佣
     * 1：bs 2:b1
     * 身份id  idcar
     * 用户id   user_id
     *
     */
    public static function newxsub($data){
        //微信支付成功后

        $osn = date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);//订单号
        $user=new BydUser();
        $sale=BydUserSale::where('user_id',$data['user_id'])->find();
        if(!empty($sale)){
            $saleedit=BydUserSale::where('user_id',$data['user_id'])->update(['level'=>$data['idcar']]);
        }
            $find=\app\admin\model\BydIden::where('id',$data['idcar'])->find();//查询身份分佣金额
//            $rand=rand(1,9999);
//            $head=$find['idenhead'];
//            $num="$head$rand";
            $num="b-" .  str_pad(rand(1,9999), 5, '0',  STR_PAD_LEFT);
            $dis=BydDistri::where('user_id',$data['user_id'])->find();//查询上级
            //$users=$user->where('id',$data['user_id'])->find();
        $users=$user->where('id',$data['user_id'])->find();
        $bbs=$users['bbs'];
            if($dis) {
                if ($bbs < 1) {
                    $save = $user->where('id', $data['user_id'])->update(['bbs' => $data['idcar']]);//修改对应的身份

                    if ($dis['family'] > 0) {



//                $father=\app\admin\model\BydUser::where('id',$data['pid'])->find();//查看父级身份
//                if($father['bbs']>0){
//                    $level=\app\admin\model\BydIdentity::find($father['bbs']);
//                    $ftc=$order['price']*$level['myproduct']/100;//上级所得提成
//                }
////                    $ftc=$order['price']*$level['myproduct']/100;//上级所得提成
//                $gfather=\app\admin\model\BydUser::where('id',$pro['family'])->find();//查看第一级别身份
//                $levels=\app\admin\model\BydIdentity::find($gfather['bbs']);

                        $pid = \app\admin\model\BydUser::find($dis['pid']);
                        $familyuser = BydUserSale::where('user_id', $dis['family'])->find();//查看上级身份
                        if ($familyuser['level'] == 3 || $familyuser['level'] == 2) {
//                        $userfind=\app\admin\model\BydUser::find($data['user_id']);
//                        if($userfind['bbs']==0){
                            if ($data['idcar'] == 1 || $data['idcar'] == 2 || $data['idcar'] == 3) {
                                //有b1上级且购买bs
                                if ($dis['family'] == $dis['pid']) {
                                    //第一级与第二级为同一人
                                    $price = BydIden::find(3);
                                    $oneprice = BydIden::find($familyuser['level']);
                                    if ($data['idcar'] == 1) {
                                        $one = $oneprice['mynewmoney'];
                                    } elseif ($data['idcar'] == 2) {
                                        $one = $oneprice['mynewmoneyt'];
                                    } else {
                                        $one = $oneprice['mynewmoneyr'];
                                    }
                                    $add = new BydDispro();
                                    $add->data([
                                        'cjprice' => $find['money'],
                                        'level' => $data['idcar'],
                                        'pid' => $dis['pid'],
                                        'state' => 1,
                                        'user_id' => $data['user_id'],
                                        'type' => 1,
                                        'order_id' => $osn,
                                        'money' => 0,
                                        'price' => $one
                                    ]);
                                    $add->save();
                                    if ($add) {
                                        $adds = new BydUserSale();
                                        $editsale = $adds->where('user_id', $data['user_id'])->update(['level' => $data['idcar'], 'bh' => $num]);

//                                    $adds->data([
//                                        'state' =>  0,
//                                        'user_id' =>  $data['user_id'],
//                                        'level'=>$data['idcar'],
//                                        'bh'=>$num
//                                    ]);
//                                    $adds->save();
                                        //$sumcom=$user->where('id',$dis['pid'])->update(['bbs'=>$data['idcar']]);//修改身份标识
                                    }

                                    //$sumcoms=$user->where('id',$dis['pid'])->setInc('cancommission',$price['mynewmoney']);//可提现用金+40
                                } else {
                                    //$familyuser=\app\admin\model\BydUser::find($dis['family']);//查看pid的身份
                                    $piduser = \app\admin\model\BydUser::find($dis['pid']);//查看pid的身份
                                    //第family上级分佣
                                    $oneprice = BydIden::find($familyuser['level']);
                                    $twoprice = BydIden::find(1);
                                    if ($data['idcar'] == 1) {
                                        $one = $oneprice['mysonmoney'];
                                        $two = $twoprice['mynewmoney'];
                                    } elseif ($data['idcar'] == 2) {
                                        $one = $oneprice['mysonmoneyt'];
                                        $two = $twoprice['mynewmoneyt'];
                                    } else {
                                        $one = $oneprice['mysonmoneyr'];
                                        $two = $twoprice['mynewmoneyr'];
                                    }
                                    if ($piduser['bbs'] == 1) {//如果为b1
                                        //第pid上级分佣

                                        $user = new BydDispro();
                                        $list = [
                                            ['pid' => $dis['family'], 'state' => 1, 'user_id' => $data['user_id'], 'type' => 1, 'order_id' => $osn, 'price' => $one, 'money' => 1,'cjprice'=>$find['money']],
                                            ['pid' => $dis['pid'], 'state' => 1, 'user_id' => $data['user_id'], 'type' => 1, 'order_id' => $osn, 'price' => $two, 'money' => 0,'cjprice'=>$find['money']]
                                        ];
                                        $user->saveAll($list);
                                        $adds = new BydUserSale();
                                        $editsale = $adds->where('user_id', $data['user_id'])->update(['level' => $data['idcar'], 'bh' => $num]);

//                                    $adds->data([
//                                        'state' =>  0,
//                                        'user_id' =>  $data['user_id'],
//                                        'level'=>$data['idcar'],
//                                        'bh'=>$num
//                                    ]);
//                                    $adds->save();
                                        //$sumcom=\app\admin\model\BydUser::where('id',$data['user_id'])->update(['bbs'=>$data['idcar']]);//修改身份标识
                                        $sumcoms = \app\admin\model\BydDistri::where('user_id', $data['user_id'])->update(['pid' => $dis['pid']]);//修改退队关系列表
                                    } elseif ($piduser['bbs'] == 2) {
                                        $twoprice = BydIden::find(2);
                                        if ($data['idcar'] == 1) {
                                            $two = $twoprice['mynewmoney'];
                                        } elseif ($data['idcar'] == 2) {
                                            $two = $twoprice['mynewmoneyt'];
                                        } else {
                                            $two = $twoprice['mynewmoneyr'];
                                        }

                                        $oneprices = BydIdentity::find(2);
                                        //第pid上级分佣
                                        $user = new BydDispro();
                                        $list = [
                                            ['pid' => $dis['family'], 'state' => 1, 'user_id' => $data['user_id'], 'type' => 1, 'order_id' => $osn, 'price' => $one, 'money' => 1,'cjprice'=>$find['money']],
                                            ['pid' => $dis['pid'], 'state' => 1, 'user_id' => $data['user_id'], 'type' => 1, 'order_id' => $osn, 'price' => $two, 'money' => 0,'cjprice'=>$find['money']]
                                        ];
                                        $user->saveAll($list);
                                        $adds = new BydUserSale();
                                        $editsale = $adds->where('user_id', $data['user_id'])->update(['level' => $data['idcar'], 'bh' => $num]);

//                                    $adds->data([
//                                        'state' =>  0,
//                                        'user_id' =>  $data['user_id'],
//                                        'level'=>$data['idcar'],
//                                        'bh'=>$num
//                                    ]);
//                                    $adds->save();
                                        //$sumcom=\app\admin\model\BydUser::where('id',$data['user_id'])->update(['bbs'=>$data['idcar']]);//修改身份标识
                                        $sumcoms = \app\admin\model\BydDistri::where('user_id', $data['user_id'])->update(['pid' => $dis['pid']]);//修改退队关系列表
                                    } elseif ($piduser['bbs'] == 3) {

                                        $twoprice = BydIden::find(3);
                                        if ($data['idcar'] == 1) {
                                            $two = $twoprice['mynewmoney'];
                                        } elseif ($data['idcar'] == 2) {
                                            $two = $twoprice['mynewmoneyt'];
                                        } else {
                                            $two = $twoprice['mynewmoneyr'];
                                        }
                                        //第pid上级分佣
                                        $user = new BydDispro();
                                        $list = [
                                            ['pid' => $dis['family'], 'state' => 1, 'user_id' => $data['user_id'], 'type' => 1, 'order_id' => $osn, 'price' => $one, 'money' => 1,'cjprice'=>$find['money']],
                                            ['pid' => $dis['pid'], 'state' => 1, 'user_id' => $data['user_id'], 'type' => 1, 'order_id' => $osn, 'price' => $two, 'money' => 0,'cjprice'=>$find['money']]
                                        ];
                                        $user->saveAll($list);
                                        $adds = new BydUserSale();
                                        $editsale = $adds->where('user_id', $data['user_id'])->update(['level' => $data['idcar'], 'bh' => $num]);

//                                    $adds->data([
//                                        'state' =>  0,
//                                        'user_id' =>  $data['user_id'],
//                                        'level'=>$data['idcar'],
//                                        'bh'=>$num
//                                    ]);
//                                    $adds->save();
                                        //$sumcom=\app\admin\model\BydUser::where('id',$data['user_id'])->update(['bbs'=>$data['idcar']]);//修改身份标识
                                        $sumcoms = \app\admin\model\BydDistri::where('user_id', $data['user_id'])->update(['pid' => $dis['pid']]);//修改退队关系列表
                                    }


//                            $upsumcom=$user->where('id',$dis['pid'])->setInc('commission',$twoprice['mynewmoney']);//上级获得总用金+30
//                            $upsumcoms=$user->where('id',$dis['pid'])->setInc('cancommission',$twoprice['mynewmoney']);//上级获得可提现用金+30
//                            $uppsumcom=$user->where('id',$dis['family'])->setInc('commission',$oneprice['mysonmoney']);//上级获得总用金+10
//                            $uppsumcoms=$user->where('id',$dis['family'])->setInc('cancommission',$oneprice['mysonmoney']);//上级获得可提现用金+10
                                }
                            } else {

                                //有b1上级且购买b1
                                $price = BydIdentity::find(2);

                                $add = new BydDispro();
                                $add->data([
                                    'cjprice' => $find['money'],
                                    'level' => $data['idcar'],
                                    'pid' => $dis['family'],
                                    'state' => 1,
                                    'user_id' => $data['user_id'],
                                    'type' => 1,
                                    'order_id' => $osn,
                                    'money' => 1,
                                    'price' => $price['mysonup']
                                ]);
                                $add->save();

                                if ($add) {

                                    $adds = new BydUserSale();
                                    $editsale = $adds->where('user_id', $data['user_id'])->update(['level' => $data['idcar'], 'bh' => $num]);

//                                $adds->data([
//                                    'state' =>  0,
//                                    'user_id' =>  $data['user_id'],
//                                    'level'=>$data['idcar'],
//                                    'bh'=>$num
//                                ]);
//                                $adds->save();
                                    $sumcom = $user->where('id', $data['user_id'])->update(['bbs' => $data['idcar']]);//修改身份标识
                                    $diss = new BydDistri();

                                    $where['family'] = array('eq', $dis['family']);
                                    $where['id'] = array('gt', $dis['id']);
                                    //$where['pid']=array('eq',$data['user_id']);
//                        $where[]=['family','=',$dis['family']];
                                    //$wheres['pid']=['eq',$data['user_id']];
                                    $pid = $diss->isUpdate(true, $where)->save(['family' => $data['user_id']]);//改下面得下级
                                    $myself = $diss->where('user_id', $data['user_id'])->update(['family' => 0, 'pid' => 0]);

                                    // $pid=$diss->where($where)->find();


                                }
//                        $uppsumcom=$user->where('id',$dis['family'])->setInc('commission',$price['mysonup']);//上级获得总用金+700
//                        $uppsumcoms=$user->where('id',$dis['family'])->setInc('cancommission',$price['mysonup']);//上级获得可提现用金+700
//                        //购买后第二级为购买者的独立成一个团队

                            }
//                        }

                        } elseif ($familyuser['level'] == 1) {
                            $piduser = \app\admin\model\BydUser::find($dis['pid']);//查看pid的身份
                            $oneprice = BydIden::find($piduser['bbs']);
//                        $userbbs=\app\admin\model\BydUser::find($data['user_id']);
//                        if($userbbs['bbs']==0){
                            if ($data['idcar'] == 1) {
                                $one = $oneprice['mynewmoney'];
                            } elseif ($data['idcar'] == 2) {
                                $one = $oneprice['mynewmoneyt'];
                            } else {
                                $one = $oneprice['mynewmoneyr'];
                            }
                            $price = BydIden::find(1);
                            $add = new BydDispro();
                            $add->data([
                                'cjprice' => $find['money'],
                                'level' => $data['idcar'],
                                'pid' => $dis['pid'],
                                'state' => 1,
                                'user_id' => $data['user_id'],
                                'type' => 1,
                                'order_id' => $osn,
                                'money' => 0,
                                'price' => $one
                            ]);
                            $add->save();
                        }

//                    }
                    else {
                        $userfind = \app\admin\model\BydUser::find($data['user_id']);
                        if ($userfind['bbs'] == 0) {
                            //c端升bs
                            $adds = new BydUserSale();
                            $adds->data([
                                'state' => 0,
                                'user_id' => $data['user_id'],
                                'level' => $data['idcar'],
                                'bh' => $num
                            ]);
                            $adds->save();
                            $sale = new BydUserSale();
                            $disfind = BydDistri::where('user_id', $data['user_id'])->update(['level' => $data['idcar']]);
                        } else {
                            $save = $user->where('id', $data['user_id'])->update(['bbs' => $data['idcar']]);//修改对应的身份
                            //散户bs升b1
                            $sale = new BydUserSale();

                            $pid = $sale->isUpdate(true, ['user_id' => $data['user_id']])->save(['level' => $data['idcar'], 'state' => 0, 'bh' => $num]);
                            $disfind = BydDistri::where('user_id', $data['user_id'])->update(['level' => $data['idcar']]);
//                    $diss=new BydDistri();
//                    //$where['pid']=array('eq',$data['user_id']);
//                    $sonfind=$diss->where('pid',$data['user_id'])->select();
//                    if(!empty($sonfind)){
//                        foreach ($sonfind as $v){
//                            $where['family']=array('eq',$v['user_id']);
//                            $familyedit=$diss->isUpdate(true,$where)->save(['family'=>$data['user_id']]);//改下面得下级
//                        }
//                    }
//                    $where['family']=array('eq',$dis['family']);
//                    $where['id']=array('gt',$dis['id']);
////                        $where[]=['family','=',$dis['family']];
//                    //$where['user_id']=['eq',$data['user_id']];
//                    $pid=$diss->isUpdate(true,$where)->save(['family'=>$data['user_id']]);//改下面得下级
                        }

                    }
                }
                }else{
                    $save=$user->where('id',$data['user_id'])->update(['bbs'=>$data['idcar']]);//修改对应的身份
                    $sale=new BydUserSale();

                    $pid=$sale->isUpdate(true,['user_id'=>$data['user_id']])->save(['level'=>$data['idcar'],'bh'=>$num]);
                    $disfind=BydDistri::where('user_id',$data['user_id'])->order('id desc')->update(['level'=>$data['idcar']]);
                }
            }else{
                $save=$user->where('id',$data['user_id'])->update(['bbs'=>$data['idcar']]);//修改对应的身份

                //散户自主购买
                if($data['idcar']==1){
                    $data['level']=1;
                    $data['state']=1;
                }else{
                    $data['level']=$data['idcar'];
                    $data['state']=0;
                }
                //添加到身份列表
                $add = new BydDistri();
                $add->data([
                    'pid'  => 0,
                    'family' =>  0,
                    'user_id' =>  $data['user_id'],
                    'level'=>$data['idcar'],
                ]);
                $add->save();
                //添加销售员列表

                $adds = new BydUserSale();
                $editsale=$adds->where('user_id',$data['user_id'])->update(['level'=>$data['idcar'],'bh'=>$num]);
//                $adds->data([
//                    'state' =>  $data['state'],
//                    'user_id' =>  $data['user_id'],
//                    'level'=>$data['idcar'],
//                    'bh'=>$num
//                ]);
//                $adds->save();
            }



    }

}